<template>
  <div class="app-container">
    <div class="app-container__section" >
      <PlusSearch
          v-bind="getSearchBinds()"
          :columns="columns"
          v-model="searchForm"
      >
      </PlusSearch>
    </div>
    <div class="app-container__section">
      <PlusTable
          v-bind="getBinds()"
          :is-selection="false"
          :columns="columns"
      >
        <template #title>
          <table-title />
        </template>
        <template #toolbar>
          <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"
              v-hasPermi="['system:post:add']"
          >添加活动</el-button>
          <ClockTimer :task="gGetList" />
          <SearchAreaControl v-model="gShowSearchForm" />
        </template>

        <template #plus-cell-status="scope">
          <dict-tag  :options="sys_normal_disable" :value="scope.row.status"></dict-tag>
        </template>
        <template #plus-cell-action="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:post:edit']">修改</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:post:remove']">删除</el-button>
        </template>
      </PlusTable>
    </div>

    <!-- 添加或修改岗位对话框 -->
    <!-- 弹窗编辑 -->
    <PlusDialogForm
        v-model:visible="dialogShow"
        v-model="formData"
        :form="{ columns,  labelWidth:'120px',  rules:formRules }"
        :dialog="{ title: dialogTitle + '活动', width: '540px', top: '12vh', confirmLoading }"
        @confirm="handleSubmit"
        @cancel="handleCancel"
    />

  </div>
</template>

<script setup name="Activity">
import { listPost, addPost, delPost, getPost, updatePost } from "@/api/system/post";
import { PlusSearch, PlusTable,PlusDialogForm } from 'plus-pro-components'
import { useTablePage } from '@/hooks/useTablePage.js'
import { useActions } from '@/hooks/useActions.js'

const { proxy } = getCurrentInstance();
const { channel_type,clues_item } = proxy.useDict("channel_type","clues_item");

// =============================================
const columns = computed(()=>[
  {
    label: '活动编号',
    prop: 'code',
    hideInSearch: true,
    hideInForm:true,
    tableColumnProps: {
      align: 'center'
    },
  },
  {
    label: '渠道来源',
    prop: 'channel',
    valueType: 'select',
    options: clues_item.value,
    tableColumnProps: {
      align: 'center'
    },
  },
  {
    label: '活动名称',
    prop: 'name',
    tableColumnProps: {
      align: 'center'
    },
  },
  {
    label: '活动简介',
    prop: 'info',
    hideInSearch: true,
    valueType: 'textarea',
    fieldProps:{
      maxlength: 200,
      showWordLimit: true,
      autosize: { minRows: 2, maxRows: 4 }
    },
    tableColumnProps: {
      align: 'center'
    },
  },
  {
    label: '活动明细',
    prop: 'status',
    hideInForm: true,
    hideInSearch:true,
    tableColumnProps: {
      align: 'center'
    },

  },
  {
    label: '开始时间',
    prop: 'beginTime',
    hideInSearch: true,
    tableColumnProps: {
      align: 'center'
    },
    valueType:'date-picker',
    fieldProps: {
      valueFormat: "YYYY-MM-DD"
    },
  },
  {
    label: '结束时间',
    prop: 'endTime',
    hideInSearch: true,
    tableColumnProps: {
      align: 'center'
    },
    valueType:'date-picker',
    fieldProps: {
      valueFormat: "YYYY-MM-DD"
    },
  },
  {
    label: '活动类型',
    prop: 'type',
    hideInTable: true,
    hideInSearch: true,
    valueType: 'select',
    options:channel_type.value,
    tableColumnProps: {
      align: 'center'
    },
  },
  {
    label: '操作',
    prop: 'action',
    width: '160px',
    hideInSearch: true,
    tableColumnProps: {
      align: 'center',
      fixed:'right'
    }
  },
])

// 搜索条件
const searchForm = ref({})

const {
  gGetList,
  gSelection,
  gShowSearchForm,
  getBinds,
  getSearchBinds
} = useTablePage(listPost, {
  searchForm,
})


const { deleteAction } = useActions();
function handleDelete(row) {
  deleteAction({
    api: delPost,
    uniqueKey:'postId',
    selection: row.postId || gSelection.value
  }).then(() => {
    gGetList();
  });
}

// =============================================
const dialogShow = ref(false)
const formData = ref({})
const formRules = []
const confirmLoading = ref(false)
const dialogTitle = computed(() => (formData.value.id ? '编辑' : '新增'))

// 新增操作
const handleAdd = ()=>{
  dialogShow.value = true
}


// 取消按钮操作
function handleCancel() {
  open.value = false;
  reset();
}
// 确定按钮操作
const handleSubmit = () => {
};

/** 表单重置 */
function reset() {
  form.value = {
    postId: undefined,
    postCode: undefined,
    postName: undefined,
    postSort: 0,
    status: "0",
    remark: undefined
  };
  proxy.resetForm("postRef");
}


/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const postId = row.postId || gSelection.value[0].postId;
  getPost(postId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改岗位";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate(valid => {
    if (valid) {
      if (form.value.postId != undefined) {
        updatePost(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          gGetList();
        });
      } else {
        addPost(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          gGetList();
        });
      }
    }
  });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/post/export", {
    ...searchForm.value
  }, `post_${new Date().getTime()}.xlsx`);
}

</script>
